package com.zhihu.matisse.internal.ui;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import com.zhihu.matisse.R;
import com.zhihu.matisse.internal.entity.Item;

public class PictureVideoPlayFragment extends Fragment implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, View.OnClickListener {
    private Item mItem = null;


    private MediaController mMediaController;
    private VideoView mVideoView;
    private ImageView iv_play;
    private int mPositionWhenPaused = -1;


    public static PictureVideoPlayFragment newInstance(Item item) {

        Bundle args = new Bundle();
        args.putParcelable("video_path", item);
        PictureVideoPlayFragment fragment = new PictureVideoPlayFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_picture_video_play, container, false);

        mItem = getArguments().getParcelable("video_path");

        mVideoView = (VideoView) rootView.findViewById(R.id.video_view);
        mVideoView.setBackgroundColor(Color.BLACK);
        iv_play = (ImageView) rootView.findViewById(R.id.iv_play);
        mMediaController = new MediaController(getActivity());
        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnPreparedListener(this);
        mVideoView.setMediaController(mMediaController);
        iv_play.setOnClickListener(this);

        return rootView;
    }


    public void onStart() {
        super.onStart();
    }

    public void onPause() {
        onFragmentPause();
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mMediaController = null;
        mVideoView = null;
    }

    public void onResume() {
        onFragmentResume();
        super.onResume();
    }

    @Override
    public boolean onError(MediaPlayer player, int arg1, int arg2) {
        return false;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        iv_play.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_play) {
            mVideoView.start();
            iv_play.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        onFragmentPause();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            onFragmentPause();
        } else {
            onFragmentResume();
        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            onFragmentResume();
        } else {
            onFragmentPause();
        }
    }

    private void onFragmentPause() {
        if (mVideoView != null && mVideoView.isPlaying()) {
            mPositionWhenPaused = mVideoView.getCurrentPosition();
            mVideoView.stopPlayback();
            iv_play.setVisibility(View.VISIBLE);
        }
    }

    private void onFragmentResume() {
        if (mVideoView != null) {
//            if (mPositionWhenPaused >= 0) {
//                mVideoView.seekTo(mPositionWhenPaused);
//                mPositionWhenPaused = -1;
//            }
            if (!mVideoView.isPlaying()) {
                mVideoView.setVideoURI(mItem.getContentUri());
                mVideoView.start();
                iv_play.setVisibility(View.INVISIBLE);
            }
        }

    }


    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                    // video started
                    mVideoView.setBackgroundColor(Color.TRANSPARENT);
                    return true;
                }
                return false;
            }
        });
    }
}
